import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
import pandas as pd
import numpy as np

# 导入数据
data = pd.read_csv(r"C:\Users\Lenovo\Desktop\Insightful & Vast USA Statistics\data_after_initsolve.csv")

# 用中位数填补缺失值
imp_median = SimpleImputer(strategy = "median")

# fit_transform一步完成调取结果
data = imp_median.fit_transform(data)

# 标准化
scaler = StandardScaler()
scaler.fit(data)
data = scaler.transform(data)
data = pd.DataFrame(data)

# 划分数据
y = data.loc[:,data.columns == 'debt']
x = data.iloc[:, 14: ]


'选择最好的n_components：累计可解释性方差贡献率曲线'
# 累计可解释性方差贡献率曲线：降维后保留特征个数为横坐标，降维后性特征矩阵捕捉到的可解释性方差贡献率为纵坐标的曲线
'选择让曲线突然变得平滑的点，本图中选'

# pca_line = PCA().fit(x)
# plt.plot(list(range(1,66)), np.cumsum(pca_line.explained_variance_ratio_))
# plt.xticks(list(range(1,66)))   # 限制坐标轴显示为整数
# plt.xlabel("降维后坐标轴数量")
# plt.ylabel("累计可解释性方差")
# plt.show()


# 调用PCA
pca = PCA(n_components=26)
pca = pca.fit(x)
X_dr = pca.transform(x)
print(X_dr)
X_dr = pd.DataFrame(X_dr)
X_dr.to_csv(r"C:\Users\Lenovo\Desktop\Insightful & Vast USA Statistics\中位数填补_PCA降维后的数据.csv")

